<?php
class Main extends Database
{
	var $config;
	
	function Main($config)
	{
		$this->config=$config;
		$this->Database(
			$this->config->mysql->host,
			$this->config->mysql->username,
			$this->config->mysql->password,
			$this->config->mysql->database,
			$this->config->mysql->perfix
		);
		
		$this->connect();
	}
	
	//update activity
	function updateActivity() {
		$update['activity'] = 'NOW()';
		$this->query_update("users", $update, "id = '" . $_SESSION['user_logged_in'] . "'");
	}
	
	// saa IP
	function getIP()
    {
		if(isset($_SERVER["HTTP_CLIENT_IP"]))
        {
			return $_SERVER["HTTP_CLIENT_IP"];
		}
		elseif(isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
        {
			return $_SERVER["HTTP_X_FORWARDED_FOR"];
		}
		elseif(isset($_SERVER["HTTP_X_FORWARDED"]))
        {
			return $_SERVER["HTTP_X_FORWARDED"];
		}
		elseif(isset($_SERVER["HTTP_FORWARDED_FOR"]))
        {
			return $_SERVER["HTTP_FORWARDED_FOR"];
		}
		elseif(isset($_SERVER["HTTP_FORWARDED"]))
        {
			return $_SERVER["HTTP_FORWARDED"];
		}
		else
        {
			return $_SERVER["REMOTE_ADDR"];
		}
	}
	
	function getAwayTime($date) {
		$time = strtotime('now')-$date;
		if($time < 3600) {
			$time = round($time / 60) . " minutit";	
		}else {
			$time = round($time / 3600);
			if($time > 24) {
				$time = round($time / 24);
				if($time == 1) {
					$time .= " päev";	
				}else {
					$time .= " päeva";	
				}
			}else {
				if($time == 1) {
					$time .= " tund";	
				}else {
					$time .= " tundi";	
				}
			}
		}
		return $time;
	}
	// Teisendame TIMESTAMP aja epoch ajaks
	function convDate($timestamp, $type=false)
	{
		preg_match("/(.*)-(.*)-(.*) (.*):(.*):(.*)/", $timestamp, $dateItems);
		
		if($type=='date')
		{
			$newDate=$dateItems[3].'.'.$dateItems[2].'.'.$dateItems[1];
		}
		else if($type=='time')
		{
			$newDate=$dateItems[4].':'.$dateItems[5].':'.$dateItems[6];
		}
		else if($type=='datetime')
		{
			$newDate=$dateItems[3].'.'.$dateItems[2].'.'.$dateItems[1].' '.$dateItems[4].':'.$dateItems[5].':'.$dateItems[6];
		}
		else
		{
			$newDate=mktime($dateItems[4], $dateItems[5], $dateItems[6], $dateItems[2], $dateItems[3], $dateItems[1]);
		}

		return $newDate;
	}
	function getUrl ($integer, $base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
	{
		$length = strlen($base);
		while($integer > $length - 1)
		{
			$out = $base[fmod($integer, $length)] . $out;
			$integer = floor( $integer / $length );
		}
		$exists = $this->query_first('SELECT id FROM banners WHERE url="' . $base[$integer] . $out . '"');
		if(empty($exists) == true) {
			return $base[$integer] . $out;
		}else {
			return $this->getUrl(mt_rand() + strtotime("now"));
		}
	}
	function formatbytes($file, $type = "KB")  
	{  
		switch($type){  
			case "KB":  
				$filesize = filesize($file) * .0009765625; // bytes to KB  
			break;  
			case "MB":  
				$filesize = (filesize($file) * .0009765625) * .0009765625; // bytes to MB  
			break;  
			case "GB":  
				$filesize = ((filesize($file) * .0009765625) * .0009765625) * .0009765625; // bytes to GB  
			break;  
		}  
		if($filesize <= 0){  
			return $filesize = 'unknown file size';
		}  else{
			return round($filesize, 2);
		}  
	} 
}
?>